//
//  PartyClassFeelingViewController.m
//  UESTCPioneer
//
//  Created by Apple on 14-5-11.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "PartyClassFeelingViewController.h"
#import "UPTableView.h"
#import "UPTitleCell.h"
#import "UPMainInfoCell.h"
#import "UPFooterCell.h"
#import "LeveyTabBarController.h"
#import "shareEditView.h"
#import "PartyThinkingReportEntity.h"
#import "commentView.h"


@interface PartyClassFeelingViewController ()<UPFooterCellDelegate,UPTitleCellDelegate>

@end

@implementation PartyClassFeelingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
//    NSLog(@"view 加载.....!");
    [super viewDidLoad];
    [self hideTopView];
    tableViewTopConstraint.constant=0;
    /*add*/
    self.title = @"党课心得";
    /**/
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //链接请求参数
    
    /*
     
     type=getNoticeOrAnnounce&userId=0004003990022&userName=xiao002&page=1&typepid=1&level=2
     */
    //    requestData=@{@"type":@"getNoticeOrAnnounce"
    //                  ,@"userId":[constant getUserId]
    //                  ,@"userName":[constant getUserName]
    //                  ,@"page":@"1"
    //                  ,@"typepid":@"1"
    //                  ,@"level":@"0"};
    //    entityName=kPartyNoticeNewsEntityName;
    //    entityMapping=[Mapping PartyNoticeMapping];
    
    
    /*
     type=getNAcomment&noticeid=13&userId=0012005130022&typepid=1&page=1
     
     */
    //    commentIdKey=@"noticeid";
    //    commentListRequestData=@{@"type":@"getNAcomment"
    //                             ,@"noticeid":@"0"
    //                             ,@"userId":[constant getUserId]
    //                             ,@"typepid":@"1"
    //                             ,@"page":@"1"};
    
    
    /*
     type=writeNAcomment&fromusername=xiao002&fromuserid=0004003990022&gonggaoid=1&content=通知的评论&typepid=1
     
     */
    //    commentWriteIdKey=@"gonggaoid";
    //    commentWriteRequestData=@{@"type":@"writeNAcomment"
    //                              ,@"fromusername":[constant getUserName]
    //                              ,@"fromuserid":[constant getUserId]
    //                              ,@"gonggaoid":@"0"
    //                              ,@"content":@""
    //                              ,@"typepid":@"1"};
    //    commentContentKey=@"content";
    //    commentListKeyMapping=@{@"commentContent":@"commentBody",@"commentAuthor":@"userName"};
    
    /*add for 党课心得*/
    /*
     请求链接的格式:
     type=sixiangpublish&userId=0012005130011&userName=tx200501&from=硕士生第五党支部&content=请支部同学交思想汇报
     说明:
     1.管理员权限
     2.发起教思想汇报的通知
     */
    
    
    /*
     请求链接的格式:
     type=getSiXiangList&userId=0012005130011&id=0&page=1
     说明:
     1.得到本支部发布的思想汇报通知列表
     */
    //commentIdKey=@"noticeid";//请求中表示Id的键，每个请求都不一样
    requestData=@{@"type":@"getSiXiangList"
                  ,@"userId":[constant getUserId]
                  ,@"id":@"0"
                  ,@"page":@"1"};
    //    entityName=kPartyNoticeNewsEntityName;
    //    entityMapping=[Mapping PartyNoticeMapping];
    entityName=kPartyThinkingReportEntity;
    entityMapping=[Mapping PartyThinkingReportEntityMapping];
    //
    /*
     请求链接的格式:
     type=sixiangSubmit&userId=0012005130040&pId=1&content=思想汇报,这段时间&name=谢艾云&userName=tx200504
     说明:
     1.提交思想汇报
     */
    //请求提交思想汇报
    commentWriteIdKey=@"pId";
    commentWriteRequestData=@{ @"type":@"sixiangSubmit"
                               ,@"userName":[constant getUserName]
                               ,@"userId":[constant getUserId]
                               ,@"pId":@"0"
                               ,@"content":@""
                               ,@"name":[constant getName]
                               };
    commentContentKey=@"content";
    commentListKeyMapping=@{@"content":@"content",@"userName":@"userName",@"time":@"time"};
    //    commentListKeyMapping=@{@"commentContent":@"commentBody",@"commentAuthor":@"userName",@"timeAndDate":@"timeAndDate"};
    //    commentWriteIdKey=@"gonggaoid";
    //    commentWriteRequestData=@{ @"type":@"writeNAcomment"
    //                              ,@"fromusername":[constant getUserName]
    //                              ,@"fromuserid":[constant getUserId]
    //                              ,@"gonggaoid":@"0"
    //                              ,@"content":@""
    //                              ,@"typepid":@"1"};
    //    commentContentKey=@"content";
    //    commentListKeyMapping=@{@"commentContent":@"commentBody",@"commentAuthor":@"userName"};
    /*
     请求链接的格式:
     type=getHuiBaoList&userId=0012005130040&pId=1&id=0&page=1
     说明:
     1.查看用户提交的思想汇报
     */
    commentIdKey=@"pId";
    commentListRequestData=@{@"type":@"getHuiBaoList"
                             ,@"userId":[constant getUserId]
                             ,@"pId":@"1"
                             ,@"id" :@"0"
                             ,@"page":@"1"
                             };
    //
    //    commentIdKey=@"noticeid";
    //    commentListRequestData=@{@"type":@"getNAcomment"
    //                             ,@"noticeid":@"0"
    //                             ,@"userId":[constant getUserId]
    //                             ,@"typepid":@"1"
    //                             ,@"page":@"1"};
    //    /*end add*/
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    maxNotification = 0;
    [self getSiXiangList];

    
    [self.navigationItem setRightBarButtonItem:Nil];
#pragma mark 加导航栏按钮
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 28, 28)];
    [imageView setImage:[UIImage imageNamed:@"write.png"]];
    [commentButton addSubview:imageView];
    
    [commentButton addTarget:self action:@selector(writeButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [commentButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 24, 24)];
    UIBarButtonItem *rightCommentBarButton=[helper BarButtonItemWithUIButton:commentButton ButtonOrigin:CGPointMake(46, 13) CustomViewSize:CGSizeMake(88, 44)];
    
    //注意，发布“交思想报告”通知只有管理员可以，管理员id末位为1或者2
    //@判断是否为管理员
    NSString *userId=[constant getUserId];
//    NSLog(@"userId = %@，userId.length = %d",userId,userId.length);
    NSString *lastChar =[userId substringFromIndex:[userId length]-1];
    
    if (!([lastChar isEqualToString:@"1"] || [lastChar isEqualToString:@"2"])){
        
    }else{
        [self.navigationItem setRightBarButtonItem:rightCommentBarButton];
    }

    /*修改*/
    self.fromSourceEnable = YES;
    /**/
}

//每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

//表的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [tableViewEntitiesArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[super tableView:tableView cellForRowAtIndexPath:indexPath];
    
    PartyThinkingReportEntity *entity = tableViewEntitiesArray[indexPath.section];
//    NSLog(@"+++++++++++++++++++++++++++++entity = %@",entity);
    self.source = entity.from;
    //    NSLog(@"党课心得  : %@",entity);
    if (indexPath.row == 0) {
        
        UPTitleCell *titleCell = (UPTitleCell*)cell;
        
        
        [titleCell setTitle:entity.userName];
        titleCell.delegate=self;
        
        [titleCell setCollectButtonEnable:NO];
        
        return cell;
    }
    else if (indexPath.row == 1) {
        UPMainInfoCell*cell2=(UPMainInfoCell*)cell;
        return cell2;
    }
    else {
        UPFooterCell*cell3=(UPFooterCell*)cell;
        cell3.delegate=self;
        
        UIButton *btn1 = (UIButton *)[cell3.contentView viewWithTag:btn1Tag];
        
        btn1.hidden = NO;
        
        return cell3;
    }
}


//修改行高度的位置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    // indexPath.section,根据分组进行更精确的配置
    return 5;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark  发布提交思想汇报通知
-(void)writeButtonPress:(commentButton*)button{
    
    //    __weak commentViewController *weakSelf=self;
    
    commentView *co=[[commentView alloc]init];
    
    [co popUpCommentViewWithCommitBlock:^(NSString *commentBody) {
        
        
        NSDictionary * personalInfo = [constant getPersonalInfo];
        NSString     * branch = [personalInfo objectForKey:@"branch"];
//        NSLog(@"branch = %@",branch);
        NSDictionary *tmpCommentRequestData=@{@"type":@"sixiangpublish",
                                              @"userId":[constant getUserId],
                                              @"userName":[constant getUserName],
                                              @"from":branch,
                                              @"comment":@""
                                              };
        
//        NSLog(@"tmpCommentRequestData = %@",tmpCommentRequestData);
        NSMutableDictionary *dic= [[NSMutableDictionary alloc]initWithDictionary:tmpCommentRequestData];
        //        [weakSelf.commentRequestData setObject:@"comment" forKey:commentBody];
        dic[commentContentKey]=commentBody;
//        NSLog(@"commentBody = %@",commentBody);
        
        NSDictionary * commentRequestData=[[NSDictionary alloc]initWithDictionary:dic];
        
        [NetworkCenter AFRequestWithData:commentRequestData SuccessBlock:^(AFHTTPRequestOperation *operation, id resultObject) {

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:resultObject options:NSJSONReadingMutableLeaves error:nil];
            if ([dic[@"result"] isEqualToString:@"success"]){
                [Alert showAlert:@"提交成功"];
                [self.tableView beginRefreshing];
            }
            else {
                [Alert showAlert:@"提交失败"];
            }
            //弹出提示：评论成功
            [co closeCommentView];
            
        } FailureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
            //弹出提示：评论失败
//            NSLog(@"提交失败");
            [Alert showAlert:@"网络请求失败!"];
        }];
    }];
}



@end
